from struct import pack, unpack
from lib.JMTBinaryProtocol import JMTBinaryProtocol
from lib.ContextSeralize import ContextSeralize
from thrift.transport import TTransport

class ContextReader(object):
    """docstring for ContextReader"""

    def __init__(self, buf, context):
        self.__buf = buf
        self.__context = context

    def read(self):
        self.readContextSerialize()

    def readContextSerialize(self):
        buf = self.__buf.read(4)
        sz = unpack('!I', buf)
        if ((sz[0] & JMTBinaryProtocol.VERSION_MASK) != JMTBinaryProtocol.VERSION_1):
            return 0

        obj = ContextSeralize(self.__context)
        transObj = TTransport.TMemoryBuffer(self.__buf)
        protocol = JMTBinaryProtocol(transObj)
        obj.read(protocol)

        # print self.__buf.read(1024)

        